<?php
// $Id: BooleanValue.class.php 73 2007-02-12 03:45:43Z tswicegood $
/**
 * This file contains {@link BooleanValue} which is part of the PHP Content 
 * Repository (phpCR), a derivative of the Java Content Repository JSR-170, and 
 * is licensed under the Apache License, Version 2.0.
 *
 * This file is based on the code created for
 * {@link http://www.jcp.org/en/jsr/detail?id=170 JSR-170}
 *
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright PHP Code Copyright &copy; 2004-2005, Domain51, United States
 * @copyright Original Java and Documentation 
 *  Copyright &copy; 2002-2004, Day Management AG, Switerland
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */

/**
 * Load the {@link phpCR} library file(s)
 */ 
require_once(dirname(__FILE__) . '/phpCR.library.php');
phpCR::loadAbstract('BaseValue');
phpCR::loadClass('PropertyType');


/**
 * A {@link BooleanValue} provides an implementation of the {@link Value} 
 * interface representing a boolean value.
 *
 * @author PHP - Travis Swicegood <travis@domain51.net>
 * @copyright Copyright &copy; 2004-2005, Domain51
 * @package phpContentRepository
 */
class BooleanValue extends BaseValue 
{
   /**
    * Defines the {@link PropertType} of this class.
    *
    * @var int
    * @see PropertyType::BOOLEAN
    */
    const TYPE = PropertyType::BOOLEAN;
    
    
   /**
    * The internal representation of this {@link BooleanValue}
    *
    * @var bool
    */
    private $bool;
    
    
   /**
    * Constructs a {@link BooleanValue} object representing a boolean.
    *
    * If this is not a boolean, it will make a best-guess conversion via
    * {@link http://www.php.net/settype settype()}
    *
    * @param bool|mixed
    */
    public function __construct($value) 
    {
        parent::__construct(self::TYPE);
        
        if (is_bool($value)) {
            $this->bool = $value;
        } elseif (settype($value, 'boolean')) {
            $this->bool = $value;
        } else {
            // Should never make it here, but just in case...
            phpCR::loadException('Repository');
            throw new RepositoryException('Unable to initialize BooleanValue');
        }
    }
    
    
   /**
    * Indicates whether some other object is "equal to" this one.
    *
    * The result is TRUE if and only if the argument is not
    * NULL and is a BooleanValue object that
    * represents the same value as this object.
    *
    * @param object
    *   The reference object with which to compare.
    * @return bool
    */
    public function equals($obj) 
    {
        if ($this == $obj) {
            return true;
        } elseif ($obj instanceof BooleanValue) {
            $objData = $obj->getData();
            if (is_null($objData)) {
                return false;
            }
            if ($this->getData() == $objData) {
                return true;
            }
        }
        return false;
    }
    
    
   /**
    * @see Value::getDate()
    */
    public function getDate()
    {
        $this->setValueConsumed();
        
        phpCR::loadException('ValueFormat');
        throw new ValueFormatException("conversion to date failed: inconvertible types");
    }
    
    
   /**
    * @see Value::getInt()
    */
    public function getInt()
    {
        $this->setValueConsumed();
        
        phpCR::loadException('ValueFormat');
        throw new ValueFormatException("conversion to int failed: inconvertible types");
    }
    
    
   /**
    * @see Value::getBoolean()
    */
    public function getBoolean()
    {
        $this->setValueConsumed();
        
        if (is_null($this->data)) { 
            return $this->data;
        } else {
            // Should never happen
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException("empty value");
        }
    }
    
    
   /**
    * @see Value::getFloat()
    */
    public function getFloat()
    {
        $this->setValueConsumed();
        
        phpCR::loadException('ValueFormat');
        throw new ValueFormatException("conversion to float failed: inconvertible types");
    }
    
    
   /**
    * @see Value::getString()
    */
    public function getString()
    {
        $this->setValueConsumed();
        
        if (!is_null($this->data)) {
            if ($this->data) {
                return 'TRUE';
            } else {
                return 'FALSE';
            }
        } else {
            // Should never happen
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException("empty value");
        }
    }
}

